package com.eCommerce.server.corebackend.dao;

import java.util.Map;


public class DAOController {
	public DAOController()
    {
    	
    }
    
    /**
     * Dictionary where we store the DAO Components of a project.
     * Any DAO Component will be store with a key. 
     * 
     */
	private Map<String,IDataAccessObject> map;
	
	
		
	/**
	 * Assign a map to the DAOController
	 * throws an exception if the map has already been set.
	 * @param value
	 */
	public void setMap(Map<String,IDataAccessObject> value) throws Exception
	{
		if(map!=null) 
		{
			String msg="The dictionary of DAO components are already load. You can not do it again...";
			Exception ex=new Exception(msg);
			throw ex;
		
		}
		this.map=value;		
	}
	
	/**
	 * Return the value associated to key in the map 
	 * @param key of the DAO Component
	 * @return DAO Component
	 */
	public IDataAccessObject get(String key) throws Exception
	{
		try
		{
			 IDataAccessObject dao = (IDataAccessObject)map.get(key);
			 if(dao==null) throw new Exception("The key -" + key + "- are not defined in DAOController");
			 return dao;
		}
		catch(Exception ex)
        {
            String exMsg = ex.getMessage();
            String msg = "Error while getting DAO component : " + key + ". DETAILS:" + exMsg;
            if (map == null)exMsg = "The Dictionry of DAO Component are empty";
            throw new Exception(msg);
        }
		
		
	}

}
